const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    banner:[],
    navHeight:'',
    page:1,
    pageSize:10,
    isEnd:false,
    contentlistdata:[],
    isloading:true,
    type:1,
    indexBtnInfodata:'',
  },
  onLoad(options) {
    this.setData({
      navHeight: app.globalData.navHeight
    })
    this.bannerList()
    this.indexBtnInfo()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
    
    
  },
  onShow(){
    this.setData({
      page:1,
      pageSize:10,
      isEnd:false,
      contentlistdata:[],
    })
    this.contentlist()
    if(wx.getStorageSync("logintoken")){
      this.upLog()
    }
  },
  bannerList(){
    Controller.bannerList({source:1}).then(res=>{
      if(res.data.baseData){
        this.setData({
          banner:res.data.baseData
        })
      }
    })
  },
  indexBtnInfo(){
    Controller.indexBtnInfo({source:1}).then(res=>{
      if(res.data.baseData){
        this.setData({
          indexBtnInfodata:res.data.baseData
        })
      }
    })
  },
  tabs(e){
    this.setData({
      type:e.currentTarget.dataset.type,
      page:1,
      pageSize:10,
      isEnd:false,
      contentlistdata:[],
    })
    this.contentlist()
  },
  golink(e){
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin()
      return
    }
    
    let title;

    if(e.currentTarget.dataset.name =='身体检测'){
        title=this.data.indexBtnInfodata['key-btn-test']
    }else if(e.currentTarget.dataset.name =='名师谈吃'){
          title=this.data.indexBtnInfodata['key-btn-doctor']
    }else if(e.currentTarget.dataset.name =='专家养生'){
        title=this.data.indexBtnInfodata['key-btn-expert']
    }else if(e.currentTarget.dataset.name =='养生视频'){
        title=this.data.indexBtnInfodata['key-btn-video']
    }else if(e.currentTarget.dataset.name =='养生食谱'){
        title=this.data.indexBtnInfodata['key-btn-recipe']
    }else if(e.currentTarget.dataset.name =='养生资讯'){
        title=this.data.indexBtnInfodata['key-btn-article']
    }

    wx.navigateTo({
      url:e.currentTarget.dataset.url+'?title='+title
    });
    
  },
  golink3(e){
    console.log(3)
    var item=e.currentTarget.dataset.item;
    if(item.type==1){
      wx.navigateTo({
        url:"/pages/news/detail?id="+item.id
      });
    }else if(item.type==2 || item.type==3){
      if(item.lessonId){
        wx.navigateTo({
          url:"/pages/videoDetails/videoDetails?id="+item.id
        });
      }else{
        wx.navigateTo({
          url:"/pages/news/detail?id="+item.id
        });
      }
    }
    
  },
  listNoLogin(){
    var data={
      keyword:"",
      source:'',
      page:this.data.page,
      pageSize:this.data.pageSize,
      categoryId:'',
      lessonId:"",
      isFollow:"",
      isRecommend:""
    }
    if(this.data.type==2){
      console.log(this.data.type)
      data.isFollow=1;
      data.isRecommend="";
    }else{
      data.isRecommend=1;
      data.isFollow="";
    }
    Controller.listNoLogin(data).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        this.setData({
          contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
        })
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })
    }).catch(res=>{

    })
  },
  contentlist(){
    var data={
      keyword:"",
      source:'',
      page:this.data.page,
      pageSize:this.data.pageSize,
      categoryId:'',
      lessonId:"",
      isFollow:"",
      isRecommend:""
    }
    if(this.data.type==2){
      console.log(this.data.type)
      data.isFollow=1;
      data.isRecommend="";
    }else{
      data.isRecommend=1;
      data.isFollow="";
    }
    if(wx.getStorageSync('logintoken')){
      Controller.contentlist(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
          })
        }
        if(res.data.baseData.lastPage){
          this.setData({
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({
          isloading:false
        })
      }).catch(res=>{

      })
    }else{
      Controller.listNoLogin(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
          })
        }
        if(res.data.baseData.lastPage){
          this.setData({
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({
          isloading:false
        })
      }).catch(res=>{

      })
    }
  },
  golink2(e){
      var item =e.currentTarget.dataset.item;
      console.log(item)

      if(item.type == 0){return;}


      if(item.type == 1 || item.type == 2||item.type == 8){
          wx.navigateTo({
            url:"/pages/news/detail?id="+item.rid
          });
      }else if(item.type == 3 ||item.type == 7){
        wx.navigateTo({
          url:"/pages/videoDetails/videoDetails?id="+item.rid
        });
      }else if(item.type == 4){
        wx.navigateTo({
          url:"/pages/mall/detail?id="+item.rid
        });
      }else if(item.type == 5){
        wx.navigateTo({
          url:"/pages/recipesDetails/recipesDetails?id="+item.rid
        });
      }else if(item.type == 6){
        // console.log(item.content)
        wx.setStorageSync('bannerContent',item.content)
        wx.navigateTo({
          url:"/pages/doc/doc?banner=true"
        });
      }else if(item.type == 9){
        wx.navigateTo({
          url:"/pages/selectMember/selectMember"
        });
      }

  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.contentlist()
    }
    
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  //登录统计
  upLog(){
    Controller.upLog({token:wx.getStorageSync('logintoken')}).then(res=>{
      // console.log('登录统计---',res)
      // if(res.data.baseData){
      //   this.setData({
      //     upLog:res.data.baseData
      //   })
      // }
    })
  }

})